﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：门店系统主窗体
//
//创建标识：齐李平 || 2011-03-24
//
//修改标识：
//修改描述：
//**********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Hmc.IM.Common;
using Hmc.IM.Common.Base;
using Hmc.IM.Common.Plugin;
using Hmc.IM.Common.UserControls;
using Hmc.IM.Client.Core;
using Hmc.IM.Client.Properties;
using System.Runtime.InteropServices;
using System.Drawing.Drawing2D;
using System.Text;
using System.Threading;


namespace Hmc.IM.Client
{
    /// <summary>
    /// 门店系统主窗体
    /// </summary>
    public partial class MainForm : Form, ILoadForm
    {
        #region 移动窗体相关事件 米海波(2011-05-11)

        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        #endregion

        #region 窗体最大化最小化 陈博生（2011-5-16）
        /// <summary>
        /// 系统菜单
        /// </summary>
        private const int WS_SYSMENU = 0x00080000;

        /// <summary>
        /// 设置窗体是否有最大最小按钮
        /// </summary>
        private const int WS_MINIMIZEBOX = 0x20000;

        /// <summary>
        /// 获得有关指定窗口的信息
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="nIndex">定要获得值的大于等于0的值的偏移量。</param>
        /// <returns>如果函数成功，返回值是所需的32位值；如果函数失败，返回值是0。</returns>
        [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
        static extern int GetWindowLong(HandleRef hWnd, int nIndex);

        /// <summary>
        /// 改变指定窗口的属性
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="nIndex">给出了要设置的值的零起点的偏移地址</param>
        /// <param name="dwNewLong">指定替换值</param>
        /// <returns>如果函数成功，返回值为给定的32位整数的原来的值。如果函数失败，返回值为0</returns>
        [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
        static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong);

        #endregion


        [DllImport("user32")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        const int AW_HOR_POSITIVE = 0x0001;
        const int AW_HOR_NEGATIVE = 0x0002;
        const int AW_VER_POSITIVE = 0x0004;
        const int AW_VER_NEGATIVE = 0x0008;
        const int AW_CENTER = 0x0010;
        const int AW_HIDE = 0x10000;
        const int AW_ACTIVATE = 0x20000;
        const int AW_SLIDE = 0x40000;
        const int AW_BLEND = 0x80000;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="frm"></param>
        public MainForm()
        {
            InitializeComponent();
            this.TransparencyKey = this.BackColor;
            panelMenuTop.MouseDown += new MouseEventHandler(panelMenuTop_MouseDown);

            #region 窗体最大化最小化 陈博生（2011-5-16）
            //获取窗体属性句柄
            int windowLong = (GetWindowLong(new HandleRef(this, this.Handle), -16));

            //设置窗体属性句柄，实现最大化最小化
            SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU | WS_MINIMIZEBOX);
            #endregion
        }

        /// <summary>
        /// 鼠标按下移动窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void panelMenuTop_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }

        #region 头部双击事件

        /// <summary>
        /// 头部双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelMenuTop_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal) pbMax_Click(sender, e);//普通窗口最大化
            else pbRestore_Click(sender, e);//最大化窗口缩小
        }

        #endregion

        #region 关闭事件

        /// <summary>
        /// 关闭按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_Click(object sender, EventArgs e)
        {
            //添加日志
            //Logger.Write("EnumSystemIdentify.Store", LoginUserEntity.SystemUserId.ToString(), LoginUserEntity.UserName, "用户退出", "用户退出");
            try
            {
                PublicVal.clientSocket.Send(Encoding.ASCII.GetBytes((int)EnumDirectiveType.Logout + "|" + LoginUserEntity.UserName + "," + CommonTools.GetLocalIP()));
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            PublicVal.clientSocket.Disconnect(false);
            PublicVal.clientSocket.Close();
            Application.Exit();
        }

        /// <summary>
        /// 关闭中代码重写
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            //添加日志
            //Logger.Write("EnumSystemIdentify.Store", LoginUserEntity.SystemUserId.ToString(), LoginUserEntity.UserName, "用户退出", "用户退出");
            base.OnClosing(e);
            Application.Exit();
        }

        #endregion

        #region 关闭按钮鼠标移动效果

        /// <summary>
        /// 关闭按钮鼠标移入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.Image = Resources.tb_4_current;
            pbClose.MouseUp -= pbClose_MouseEnter;
        }

        /// <summary>
        /// 关闭按钮鼠标移出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.Image = Resources.tb_close;
            pbClose.MouseUp -= pbClose_MouseLeave;
        }

        #endregion

        #region 最大化鼠标移动效果和最大化效果

        /// <summary>
        /// 最大化鼠标移进效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMax_MouseEnter(object sender, EventArgs e)
        {
            pbMax.Image = Resources.tb_max_current;
        }

        /// <summary>
        /// 最大化鼠标移出效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMax_MouseLeave(object sender, EventArgs e)
        {
            pbMax.Image = Resources.tb_max1;
        }

        /// <summary>
        /// 最大化鼠标点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMax_Click(object sender, EventArgs e)
        {
            this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
            this.WindowState = FormWindowState.Maximized;
            pbMax.Image = Resources.tb_restore;
            pbMax.Click -= pbMax_Click;
            pbMax.MouseEnter -= pbMax_MouseEnter;
            pbMax.MouseLeave -= pbMax_MouseLeave;
            pbMax.Click += new EventHandler(pbRestore_Click);
            pbMax.MouseEnter += pbRestore_MouseEnter;
            pbMax.MouseLeave += pbRestore_MouseLeave;
        }

        #endregion

        #region 还原鼠标移动效果和还原效果

        /// <summary>
        /// 还原按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbRestore_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            pbMax.Image = Resources.tb_max1;
            pbMax.Click -= pbRestore_Click;

            pbMax.MouseEnter -= pbRestore_MouseEnter;
            pbMax.MouseLeave -= pbRestore_MouseLeave;
            pbMax.Click += new EventHandler(pbMax_Click);
            pbMax.MouseEnter += pbMax_MouseEnter;
            pbMax.MouseLeave += pbMax_MouseLeave;
        }

        /// <summary>
        /// 还原鼠标移进效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbRestore_MouseEnter(object sender, EventArgs e)
        {
            pbMax.Image = Resources.tb_restore_;
        }

        /// <summary>
        /// 还原鼠标移出效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbRestore_MouseLeave(object sender, EventArgs e)
        {
            pbMax.Image = Resources.tb_restore;
        }

        #endregion

        #region 最小化鼠标事件

        /// <summary>
        /// 最小化鼠标移入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMin_MouseEnter(object sender, EventArgs e)
        {
            pbMin.Image = Resources.tb_min_current;
        }

        /// <summary>
        /// 最小化鼠标移出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMin_MouseLeave(object sender, EventArgs e)
        {
            pbMin.Image = Resources.tb_min;
        }

        /// <summary>
        /// 最小化点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        #endregion

        #region 帮助菜单鼠标移动事件

        /// <summary>
        /// 帮助菜单鼠标移入事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbHelp_MouseEnter(object sender, EventArgs e)
        {
            pbHelp.Image = Resources.tb_help_current;
        }

        /// <summary>
        /// 帮助菜单鼠标移出事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbHelp_MouseLeave(object sender, EventArgs e)
        {
            pbHelp.Image = Resources.tb_help;
        }

        #endregion

        #region 窗体加载初始化

        /// <summary>
        /// Form_Load事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            lblState.Text = "当前操作用户：" + LoginUserEntity.UserName;
            if (!string.IsNullOrEmpty(LoginUserEntity.RoleName))
            {
                lblState.Text += "[" + LoginUserEntity.RoleName + "]";
            }
            else
            {
                lblState.Text += "[系统初始化帐号]";
            }
            lblVersion.Text = "欢迎使用华贸城客户端，主程序版本：V" + this.ProductVersion; ;
            labelTitle.Text = LoginUserEntity.OrganizationName + "-" + CommonTools.SoftName + CommonTools.SoftVersion;
            //加载插件
            //Core.PluginLoader.LoadAllPlugins();

            //加载插件菜单
            //InitPlugin();

            LoadDefaultNav();//加载默认子菜单
            LoadDefaultForm();//加载默认Form


            #region 第一次加载信息提醒
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            worker.RunWorkerAsync(); 
            #endregion

            //启动发送心跳信息
            Thread threadSend = new Thread(SendHandClaspMsg);
            threadSend.Start();

            //启动接收服务器端信息（包括心跳包）
            Thread threadReceive = new Thread(ReceiveData);
            threadReceive.Start();
            //while (true)
            //{
            //    //加载提醒信息
            //    //BackgroundWorker workert = new BackgroundWorker();
            //    //workert.DoWork += new DoWorkEventHandler(worker_DoWork);
            //    //workert.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            //    //workert.RunWorkerAsync();
            //    Thread.Sleep(5000);
            //}
        }

        /// <summary>
        /// 心跳包
        /// </summary>
        private void SendHandClaspMsg()
        {
            while (true)
            {
                Thread.Sleep(60000);//1分钟发送一次
                if (PublicVal.clientSocket == null || PublicVal.clientSocket.Connected == false) break;
                try
                {
                    PublicVal.clientSocket.Send(Encoding.ASCII.GetBytes((int)EnumDirectiveType.HandClasp + "|" + LoginUserEntity.UserName + "," + CommonTools.GetLocalIP()));
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    break;
                }
            }
        }

        /// <summary>
        /// 接收消息（包括心跳包）
        /// </summary>
        private void ReceiveData()
        {
            byte[] receiveResult = new byte[1024];
            while (true)
            {
                if (PublicVal.clientSocket == null || PublicVal.clientSocket.Connected == false) break;

                try
                {
                    //通过clientSocket接收数据  
                    int receiveLength = PublicVal.clientSocket.Receive(receiveResult);
                    if (receiveLength > 0)
                    {
                        string resultStr = Encoding.ASCII.GetString(receiveResult, 0, receiveLength);

                        CommandResultEntity result = CommandBusiness.Execute(resultStr);
                        if (result.Result == true)
                        {
                            int orderCounts = 0;
                            int.TryParse(result.Message, out orderCounts);

                            if (orderCounts > 0)
                            {
                                //一、配置（不配置采用默认设置）：

                                //1.自定义配置方式1
                                NoticeCenter.Cfg.Height = 140;//弹出框高
                                NoticeCenter.Cfg.Width = 250;//弹出框宽
                                NoticeCenter.Cfg.Overlap = false;//弹出框是否重叠，true时弹出的框位置一致，后面弹出的将遮挡前面弹出的
                                NoticeCenter.Cfg.TotalShow = 5;//同一时刻显示几个提示框，如果超出5个，该线程将等待前面的线程释放

                                //2.自定义配置方式2
                                NoticeCenter.Cfg = new NoticeCfg { Height = 140, Width = 250, TotalShow = 5, Overlap = false };

                                //二、创建“通知”

                                //3.detail里的内容就是用户点击“查看详细”时弹出的框中显示的内容，用于弹出框不适合显示太多文本用一个详细窗口来显示具体内容
                                Notice notice_3 = new Notice { msg = "   您有" + orderCounts + "条新订单等待处理，请及时处理！", wait = 3000 };

                                //三、弹出通知
                                NoticeCenter.Show(notice_3);//将要弹出的消息放入队列，待轮到该提示弹出时将弹出
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    MessageBox.Show(ex.Message+"请重新登录！");
                    break;
                }
            }
        }

        #region 第一次加载信息提醒
        /// <summary>
        /// 登录线程处理完毕执行方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //1.自定义配置方式1
            NoticeCenter.Cfg.Height = 140;//弹出框高
            NoticeCenter.Cfg.Width = 250;//弹出框宽
            NoticeCenter.Cfg.Overlap = false;//弹出框是否重叠，true时弹出的框位置一致，后面弹出的将遮挡前面弹出的
            NoticeCenter.Cfg.TotalShow = 5;//同一时刻显示几个提示框，如果超出5个，该线程将等待前面的线程释放

            //2.自定义配置方式2
            NoticeCenter.Cfg = new NoticeCfg { Height = 140, Width = 250, TotalShow = 5, Overlap = false };

            //二、创建“通知”

            //3.detail里的内容就是用户点击“查看详细”时弹出的框中显示的内容，用于弹出框不适合显示太多文本用一个详细窗口来显示具体内容
            Notice notice_3 = new Notice { msg = "   " + (string)e.Result, wait = 3000 };

            //三、弹出通知
            NoticeCenter.Show(notice_3);//将要弹出的消息放入队列，待轮到该提示弹出时将弹出


            BackgroundWorker worker = sender as BackgroundWorker;

            worker.Dispose();
        }
        /// <summary>
        /// 处理订单提醒信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {

            //while (true)
            //{
            byte[] receiveResult = new byte[1024];
            //通过clientSocket接收数据  
            int receiveLength = PublicVal.clientSocket.Receive(receiveResult);

            if (receiveLength > 0)
            {
                string resultStr = Encoding.ASCII.GetString(receiveResult, 0, receiveLength);

                CommandResultEntity result = CommandBusiness.Execute(resultStr);
                if (result.Result == true)
                {
                    int orderCounts = 0;
                    int.TryParse(result.Message, out orderCounts);
                    e.Result = "上次退出后有" + orderCounts + "条新订单等待处理，请及时处理！";
                }
            }
            else
            {
                e.Result = "没有新订单";
                //MessageBox.Show("没有新订单");
            }
            //Thread.Sleep(2000);
            //}

        } 
        #endregion

        /// <summary>
        /// 加载默认导航菜单
        /// </summary>
        private void LoadDefaultNav()
        {
            panelNavMenu.Controls.Clear();
            PluginUIHelper.CreateChildMenu("首页", true, panelNavMenu);
        }

        /// <summary>
        /// 加载默认Form
        /// </summary>
        private void LoadDefaultForm()
        {
            Utility.ShowFormInControl(new HomePageForm(), panelMainCenter, true);
        }

        /// <summary>
        /// 加载插件菜单
        /// </summary>
        public void InitPlugin()
        {
            this.panelPluginMenu.Controls.Clear();
            foreach (IPlugin plugin in PluginLoader.plugins)
            {
                var query = from p in LoginUserEntity.PermissionsPoint where plugin.ModuleId == p select p;
                if (query == null || query.Count() == 0)
                {
                    //添加日志
                    //Logger.Write("EnumSystemIdentify.Store", LoginUserEntity.SystemUserId.ToString(), LoginUserEntity.UserName, "加载插件菜单", "无权限加载“" + plugin.ModuleName + "”");
                    continue;//无权限退出
                }
                plugin.FormLoader = this;
                //加载菜单到panelPluginMenu
                panelPluginMenu.Controls.Add(PluginUIHelper.InitMenuUI(plugin));
            }
        }

        #endregion

        #region “首页”菜单点击事件

        /// <summary>
        /// pbHomeMenu菜单点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pbHomeMenu_Click(object sender, EventArgs e)
        {
            LoadDefaultNav();//加载菜单
            LoadDefaultForm();//加载主体内容窗口

            PictureBox pictureBox = sender as PictureBox;
            foreach (Control c in panelPluginMenu.Controls)
            {
                c.BackgroundImage = null;
                PictureBox tempPic = c.Controls[0] as PictureBox;

                tempPic.MouseLeave += PluginUIHelper.picture_MouseLeave;
                tempPic.MouseEnter += PluginUIHelper.picture_MouseEnter;
            }
            //给首页模块菜单添加背景
            panelHomeMenu.BackgroundImage = Resources.nav_on;

            //清除首页模块菜单事件
            EventHelper.ClearEvent(pbHomeMenu, "MouseEnter");
            EventHelper.ClearEvent(pbHomeMenu, "MouseLeave");
            EventHelper.ClearEvent(pbHomeMenu, "Click");
        }

        #endregion

        #region 实现接口加载方法

        /// <summary>
        /// 加载内容Form
        /// </summary>
        /// <param name="sender">事件对象</param>
        /// <param name="frm">需要加载的窗体</param>
        public void LoadContentForm(object sender, Form frm)
        {
            #region 子菜单点击选中处理
            if (sender.GetType() == typeof(ucMenuButton))
            {
                ucMenuButton menu = sender as ucMenuButton;
                foreach (Control c in panelNavMenu.Controls)
                {
                    if (c.GetType() != typeof(ucMenuButton)) continue;

                    if (c == menu)
                    {
                        //当前选择的菜单
                        ((ucMenuButton)c).Image = Resources.menu_current_bg;
                        ((ucMenuButton)c).TextColor = Color.FromArgb(4, 91, 196);
                    }
                    else
                    {
                        //非当前选择的菜单
                        ((ucMenuButton)c).Image = Resources.menu_bg;
                        ((ucMenuButton)c).TextColor = Color.FromArgb(48, 48, 48);
                    }
                }
            }

            #endregion

            try
            {
                IBaseForm f = frm as IBaseForm;

                //权限判断，无权限退出
                if (Utility.HadPermissions(f.PowerId) == false) return;

                //显示窗体
                Utility.ShowFormInControl(frm, panelMainCenter, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //添加异常日志
                //Recorder.Write(ex);
            }
        }

        /// <summary>
        /// 加载子菜单
        /// </summary>
        /// <param name="menus">菜单集合</param>
        public void LoadMenu(List<PluginChildMenuEntity> menus)
        {
            panelNavMenu.Controls.Clear();

            for (int i = 0; i < menus.Count; i++)
            {
                //权限控制判断
                if (Utility.HadPermissions(menus[i].MenuPowerId) == false) continue;

                //添加子菜单
                PluginUIHelper.CreateChildMenu(menus[i].MenuName, i == 0, panelNavMenu, menus[i].ClickEvent);
            }
        }

        /// <summary>
        /// 模块菜单点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void MenuClickEvent(object sender, EventArgs e)
        {
            #region 消毁所有内容窗体

            for (int i = 0; i < panelMainCenter.Controls.Count; i++)
            {
                var temp = panelMainCenter.Controls[i];
                //添加日志
                //Logger.Write("EnumSystemIdentify.Store", LoginUserEntity.SystemUserId.ToString(), LoginUserEntity.UserName, "消毁" + temp.Text + "窗体", "消毁" + temp.Text + "窗体");
                temp.Dispose();
            }
            panelMainCenter.Controls.Clear();

            #endregion

            #region 模块菜单状态变化操作
            PictureBox pictureBox = sender as PictureBox;
            Panel tempPanel = (Panel)pictureBox.Parent;
            tempPanel.BackgroundImage = Resources.nav_on;

            foreach (Control c in panelPluginMenu.Controls)
            {
                if (c.GetType() == typeof(Panel) && c != tempPanel)
                {
                    c.BackgroundImage = null;
                    PictureBox tempPic = c.Controls[0] as PictureBox;

                    tempPic.MouseLeave += PluginUIHelper.picture_MouseLeave;
                    tempPic.MouseEnter += PluginUIHelper.picture_MouseEnter;
                }
                else
                {
                    EventHelper.ClearEvent(pictureBox, "MouseLeave");
                    EventHelper.ClearEvent(pictureBox, "MouseEnter");
                }
            }
            #endregion

            //给首页菜单添加事件
            panelHomeMenu.BackgroundImage = null;
            pbHomeMenu.MouseEnter += PluginUIHelper.picture_MouseEnter;
            pbHomeMenu.MouseLeave += PluginUIHelper.picture_MouseLeave;
            pbHomeMenu.Click += new EventHandler(pbHomeMenu_Click);
        }

        #endregion

        #region 帮助菜单
        /// <summary>
        /// 判断点击的鼠标是左键还是右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbHelp_MouseDown(object sender, MouseEventArgs e)
        {
            //如果是单击的是左键
            if (e.Button == MouseButtons.Left)
            {
                contextMenuStrip1.Paint += new PaintEventHandler(contextMenuStrip1_Paint);
                contextMenuStrip1.Tag = true;
                contextMenuStrip1.Show((PictureBox)sender, new Point(e.X - 25, 18)); //在你单击的地方弹出菜单
            }
            if (e.Button == MouseButtons.Right)
            {
                contextMenuStrip1.Tag = false;
            }
        }

        void contextMenuStrip1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rect = new Rectangle(0, 0, contextMenuStrip1.Width, contextMenuStrip1.Height);
            LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rect,
                Color.FromArgb(255, 35, 152, 205), Color.FromArgb(255, 216, 241, 246), LinearGradientMode.Vertical);
            e.Graphics.FillRectangle(myLinearGradientBrush, rect);
        }

        /// <summary>
        /// 根据左右键判断是否取消菜单弹出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (!Convert.ToBoolean(contextMenuStrip1.Tag))
            {
                e.Cancel = true;
            }
        }

        /// <summary>
        /// 我的帐号菜单点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmMyAccount_Click(object sender, EventArgs e)
        {
            foreach (Control c in panelPluginMenu.Controls)
            {
                if (c.GetType() == typeof(Panel))
                {
                    c.BackgroundImage = null;
                    PictureBox tempPic = c.Controls[0] as PictureBox;
                    //先清除事件（以免重复）
                    EventHelper.ClearEvent(tempPic, "MouseLeave");
                    EventHelper.ClearEvent(tempPic, "MouseEnter");
                    //重新添加事件
                    tempPic.MouseLeave += PluginUIHelper.picture_MouseLeave;
                    tempPic.MouseEnter += PluginUIHelper.picture_MouseEnter;
                }
            }

            //给首页菜单添加背景
            panelHomeMenu.BackgroundImage = Resources.nav_on; ;
            //先清除点击事件,以免重复
            EventHelper.ClearEvent(pbHomeMenu, "Click");

            //重新添加点击事件
            pbHomeMenu.Click += new EventHandler(pbHomeMenu_Click);

            //加载我的帐号菜单
            MyAccountMenus.FormLoader = this;
            LoadMenu(MyAccountMenus.Menus);

            //加载我的帐号窗体
            Utility.ShowFormInControl(new MyAccoutForm(), this.panelMainCenter, true);
        }

        /// <summary>
        /// 帮助按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmHelp_Click(object sender, EventArgs e)
        {
            MessageBox.Show("帮助功能暂未开放！");
        }

        /// <summary>
        /// 关于按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show(CommonTools.SoftName + "V" + this.ProductVersion + "\r\n \r\n开发小组：波特集团技术一部" + "\r\n \r\n技术支持：400 655 0037");
        }

        /// <summary>
        /// 注销按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmLogout_Click(object sender, EventArgs e)
        {
            //添加日志
            //Logger.Write("EnumSystemIdentify.Store", LoginUserEntity.SystemUserId.ToString(), LoginUserEntity.UserName, "用户注销", "用户注销");
            LoginUserEntity.Token = "";
            LoginUserEntity.SystemUserId = Guid.Empty;
            LoginUserEntity.OrganizationId = Guid.Empty;
            //清楚插件
            Core.PluginLoader.plugins.Clear();
            //清楚控件
            this.panelMenu.Controls.Clear();
            this.panelPluginMenu.Controls.Clear();
            this.Controls.Clear();
            this.Hide();
            this.Dispose();
            //回到登录界面
            LoginForm frm = new LoginForm();
            frm.Show();

            //Application.Restart();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmEsc_Click(object sender, EventArgs e)
        {
            //添加日志
            //Logger.Write("EnumSystemIdentify.Store", LoginUserEntity.SystemUserId.ToString(), LoginUserEntity.UserName, "用户退出", "用户退出");
            Application.Exit();
        }

        #endregion

    }
}
